<?php defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );

    class Privilege extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            //加载验证码
            $this->load->helper( 'captcha' );
        }

        /**
         * 登录界面
         */
        public function login()
        {
            #如果登录则直接到后台
            $admin_id = $this->session->userdata( 'admin_id' );
            if ( $admin_id ) redirect( "admin/admin/index" );

            $vals = [
                'word'       => rand( 1000, 9999 ),
                'img_path'   => './data/captcha/',
                'img_url'    => base_url() . 'data/captcha/',
                'img_width'  => '150',
                'img_height' => 25,
                'expiration' => 60
            ];
            $data = create_captcha( $vals );

            $this->load->view( "login.html", $data );
        }

        /**
         *登录处理
         */
        public function dologin()
        {
            #接受参数
            $aname = $this->input->post( 'aname' );
            $apassword = $this->input->post( 'apassword' );
            #操作处理
            $this->load->model( "admin/admin_model" );
            $login_info = $this->admin_model->login( [ 'name' => $aname ], $apassword );
            if ( !$login_info[ 'isok' ] ) P::exitError( $login_info );
            #session数据处理
            //管理员ID
            $this->session->set_userdata( 'admin_id', $login_info[ 'data' ][ 'id' ] );
            //管理员名
            $this->session->set_userdata( 'admin_name', $_POST[ 'aname' ] );
            //管理员所属组
            $this->session->set_userdata( 'admin_groups', $login_info[ 'data' ][ 'groups' ] );
            //超级组ID
            $this->session->set_userdata( 'super_group', $login_info[ 'data' ][ 'super_group' ] );
            //是否属于超级组
            $this->session->set_userdata( 'in_super_group', $login_info[ 'data' ][ 'in_super_group' ] );

            P::exitOk();
        }

        /**
         * 退出系统
         */
        public function loginOut()
        {
            $this->session->sess_destroy();
            redirect( "admin/privilege/login" );
        }

        //生成验证码
        public function makeCode()
        {

        }
    }

?>